package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk/api/result"
	"zk/internal/dao"
	"zk/internal/model"
)

func LiveRoomAdd(c *gin.Context) {
	var req result.LiveRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}

	live_room := model.LiveRoom{
		RoomName:     req.RoomName,
		RoomTitle:    req.RoomTitle,
		RoomBrief:    req.RoomBrief,
		RoomDescribe: req.RoomDescribe,
	}
	if !dao.CreateLiveRoom(&live_room) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"date": live_room,
	})
}

func LiveRoomList(c *gin.Context) {
	var req result.LiveRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}

	live_room := model.LiveRoom{
		RoomName:     req.RoomName,
		RoomTitle:    req.RoomTitle,
		RoomBrief:    req.RoomBrief,
		RoomDescribe: req.RoomDescribe,
	}
	if !dao.CreateLiveRoom(&live_room) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"date": live_room,
	})
}

func LiveRoomUpdate(c *gin.Context) {
	var req result.UpdateLiveRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	room := dao.GetUpdateLiveRoom(req.Id, &model.LiveRoom{})
	if room.ID == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "修改成功",
			"date": room,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"date": room,
	})
}

func LiveRoomDelete(c *gin.Context) {
	var req result.DeleteLiveRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	room := dao.GetLiveRoomById(req.Id)
	if room.ID != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "直播间不存在",
			"date": nil,
		})
		return
	}
	if !dao.GetDeleteLiveRoom(model.LiveRoom{}) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "直播间删除成功",
		"date": room,
	})
}
